package leetcode.lt100;

import java.util.*;

class Solution_90 {

    public static void main(String[] args) {
        int[] nums = {1, 2, 2};
        subsetsWithDup(nums);
    }

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(nums, 0, res, list);
        return res;
    }

    private static void dfs(int[] nums, int index, List<List<Integer>> res, List<Integer> temp) {
        res.add(new ArrayList<>(temp));
        for (int i = index; i < nums.length; i++) {
            //和上个数字相等就跳过
            if (i > index && nums[i] == nums[i - 1]) {
                continue;
            }
            temp.add(nums[i]);
            dfs(nums, i + 1, res, temp);
            temp.remove(temp.size() - 1);
        }
    }

}